package edu.ccut.saturn.authority.impl.login;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IAuthorityResourceInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnAuthorityManager;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDataManager;

public class GetResourceList extends DefaultComponent {

	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		List<SaturnData> amRoleResourceList = (List<SaturnData>) parameterContext
				.getParameterValue(0);
		List<SaturnData> ResourceList = new ArrayList<SaturnData>();

		List<IAuthorityResourceInfo> allResourceList = SaturnAuthorityManager
				.getInstance().getAllResources();

		if (amRoleResourceList != null) {
			for (SaturnData amRoleResource : amRoleResourceList) {
				String resource_serialno = amRoleResource
						.getString("resource_id");
				for (IAuthorityResourceInfo resourceInfo : allResourceList) {
					if (resource_serialno.equals(resourceInfo.getResourceKey())) {
						SaturnData resource = SaturnDataManager
								.getInstance()
								.createData(
										"http://edu.ccut.saturn.authority.core/commonModule/data",
										"SATURN_AUTHORITY_RESOURCE");
						resource.setString("id", resource_serialno);
						resource.setString("name", resourceInfo.getName());
						resource.setString("url", resourceInfo.getValue());
						resource.setString("descn", resourceInfo
								.getDescription());
						ResourceList.add(resource);
					}
				}
			}
		}

		parameterContext.setParameter(1, ResourceList);
	}
}